<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>Lins_mall后台管理系统</span>
      </div>
      <el-button type="danger" @click="logOut">退出登录</el-button></el-header
    >
    <el-container>
      <el-aside :width=" isCollapse ? '64px' : '200px'">
      <div class="show_all" @click="showAll">|||</div>
        <!-- 侧边栏菜单
        unique-opened 是否只保持一个子菜单的展开
        collapse 是否水平折叠收起菜单
        collapse-transition 是否开启折叠动画
        router 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转
        default-active 当前激活菜单的 index(改变颜色)
        -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition ='false'
          :router="true"
          :default-active="activeIndex"
        >
          <!-- 一级菜单  index值控制同一index的选项同时展开和收起 需要传一个字符串 -->
          <el-submenu :index="index+''" v-for="(item,index) in menuList" :key="index">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 小图标 -->
              <i :class="icon[index]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单  开启了router模式，点击后index的值会添加到url里面去-->
            <el-menu-item :index="'/'+ziitem.path " v-for="ziitem in item.children" :key="ziitem.id" @click="changeActive(ziitem.path)">
              <!-- 二级菜单模板区 -->
              <template slot="title">
              <!-- 小图标 -->
              <i class="el-icon-orange"></i>
              <span>{{ziitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 显示子路由视图的占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单栏的数据
      menuList: [],
      // 图标数据
      icon: ['iconfont icon-user', 'iconfont icon-tijikongjian', 'iconfont icon-shangpin', 'iconfont icon-danju', 'iconfont icon-baobiao'],
      isCollapse: false,
      // 当前高亮的index
      activeIndex: ''
    }
  },
  created () {
    this.getMenuList()
    // 页面刷新的时候，拿出保存的当前高亮的index
    this.activeIndex = window.sessionStorage.getItem('activeIndex')
  },
  methods: {
    logOut () {
      //  window.sessionStorage.clear()
      window.sessionStorage.removeItem('token')
      this.$router.push('/Login')
    },
    // 拿到左边导航栏数据
    getMenuList () {
      this.$http.get('menus').then(result => {
        // result里面的data的value赋值给左边data 并且重命名为res
        const { data: res } = result
        console.log('menu数据是', res)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        // 拿到左侧数据
        this.menuList = res.data
        this.menuList = [...this.menuList]
        console.log('左侧菜单数据是', this.menuList)
      })
    },
    // 改变菜单的折叠或者展开状态
    showAll () {
      this.isCollapse = !this.isCollapse
    },
    // 左侧菜单高亮显示
    changeActive (path) {
      this.activeIndex = '/' + path
      // 也可以this.activeIndex = this.$route.path
      // 存进sessionStorge中 刷新后还会继续保存
      window.sessionStorage.setItem('activeIndex', '/' + path)
    }
  }

}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
// elementui的標籤就是一個類
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  img {
    width: 50px;
  }
  padding: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  // less语法 子div
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
  margin-right: 10px;
}
.show_all{
  background-color: #333733;
  color: #fff;
  text-align: center;
  letter-spacing: 2px;
  font-size: 14px;
  line-height: 14px;
  cursor: pointer;
}
</style>
